import * as Redux from 'redux';
import {
    Action,
    defaultActionParam,
    ActionParam,

} from '../../action';

export const Init = 'model-common-初始化';
export const Fetch_Init = 'init';
export const initAction = (other = defaultActionParam): Action => ({
    type: Init,
    payload: {
        fetch: Fetch_Init,
        mandatory: other.mandatory,
    },
    UUID: other.UUID,
});

export interface ConnectedModelCommonDispatch {
    _m_common_init(other?: ActionParam): Promise<Action>;
}

export const connect = (dispatch: Redux.Dispatch<any>, connected: any, mapKeys: any): ConnectedModelCommonDispatch => ({
    ...connected,
    _m_common_init: (other: any) => dispatch(initAction(other)),
});

export interface Common {

}

const defaultState: Common = {};

const ACTIONS_HANDLER: any = {};

export function commonReducer(state = defaultState, action: Action): Common {
    const handler = ACTIONS_HANDLER[action.type];
    return handler ? handler(state, action) : state;
}